#----------------------------------------------------------------------------
# Setup the project
cmake_minimum_required(VERSION 3.16...3.27)
if(${CMAKE_VERSION} VERSION_LESS 3.12)
    cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
endif()

project(g4test LANGUAGES CXX)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 17)

#000000000000000000000000000000000000 检查是否在 conan2 中构建
# message(DEBUG "[conan2] ${CMAKE_TOOLCHAIN_FILE} = ${CMAKE_TOOLCHAIN_FILE}")
set(IN_CONAN_BUILD FALSE)
if(EXISTS "${CMAKE_TOOLCHAIN_FILE}")
    get_filename_component(TOOLCHAIN_NAME "${CMAKE_TOOLCHAIN_FILE}" NAME)
    if(TOOLCHAIN_NAME STREQUAL "conan_toolchain.cmake")
        message(STATUS ">>> Running under Conan 2 environment")
        set(IN_CONAN_BUILD TRUE)
    endif()
endif()

#===========================================
if(WIN32)
    add_compile_definitions(WIN32)
    list(PREPEND CMAKE_PREFIX_PATH "C:/cpplibs/geant4-11.3.2/lib/cmake/Geant4")
elseif(LINUX)
    list(PREPEND CMAKE_PREFIX_PATH "$ENV{HOME}/cpplibs/geant4v11.3.2")
endif(WIN32)

set(myEXE ${PROJECT_NAME})

#=========================================== conan2 兼容设置
# conan2 的 expat 包的 target 是小写的 expat::expat;
# geant4 期待大写的 EXPAT::EXPAT
if(IN_CONAN_BUILD)
    find_package(expat REQUIRED)
    add_library(EXPAT::EXPAT ALIAS expat::expat)
endif()

#----------------------------------------------------------------------------
# Find Geant4 package, activating all available UI and Vis drivers by default
# You can set WITH_GEANT4_UIVIS to OFF via the command line or ccmake/cmake-gui
# to build a batch mode only executable
#
option(WITH_GEANT4_UIVIS "Build example with Geant4 UI and Vis drivers" ON)
if(WITH_GEANT4_UIVIS)
    find_package(Geant4 REQUIRED ui_all vis_all)
else()
    find_package(Geant4 REQUIRED)
endif()
message(DEBUG "[EXPAT] Geant4_builtin_expat_FOUND = ${Geant4_builtin_expat_FOUND}")
message(DEBUG "[EXPAT] ${Geant4_LIBRARIES} = ${${Geant4_LIBRARIES}}")


#----------------------------------------------------------------------------
# Setup Geant4 include directories and compile definitions
#
include(${Geant4_USE_FILE})

#----------------------------------------------------------------------------
# Locate sources and headers for this project
#
include_directories(${PROJECT_SOURCE_DIR}/include
    ${Geant4_INCLUDE_DIR})
file(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cc)
file(GLOB headers ${PROJECT_SOURCE_DIR}/include/*.hh)

#----------------------------------------------------------------------------
# Add the executable, and link it to the Geant4 libraries
#
add_executable(${myEXE} g4test.cc ${sources} ${headers})
target_link_libraries(${myEXE} ${Geant4_LIBRARIES})

#----------------------------------------------------------------------------
# Install the executable to 'bin' directory under CMAKE_INSTALL_PREFIX
#
install(TARGETS ${myEXE} DESTINATION bin)


#----------------------------------------------------------------------------
# 将所有 scripts 拷贝到 build 目录. exe 运行的时候，会从所在目录寻找 scripts.
set(EXAMPLEB1_SCRIPTS
    init_vis.mac
    vis.mac
    letd.mac
    qa1.mac
)

foreach(_script ${EXAMPLEB1_SCRIPTS})
    configure_file(
        ${PROJECT_SOURCE_DIR}/${_script}
        ${PROJECT_BINARY_DIR}/${_script}
        COPYONLY
    )
endforeach()
